package com.cityfood.common.utils;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.cityfood.common.pojo.AccessToken;
import com.cityfood.common.pojo.ResultTemplateDate;

/**
 * 微信服务通知工具类
 * */
public class Notificationtuils {
	
	
	
	/**
	 * 根据微信id，secret获取access_token
	 * 
	 */
	public String getAccessToken() {
		String tmpUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + CheckUserUtils.APPID
				+ "&secret=" + CheckUserUtils.APPPSECRET + "";
		CloseableHttpClient httpCilent = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(tmpUrl);
		try {
			HttpResponse httpResponse = httpCilent.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String entity = EntityUtils.toString(httpResponse.getEntity());
				AccessToken accessToken = JSONObject.parseObject(entity, AccessToken.class);
				return accessToken.getAccess_token();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				httpCilent.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";
	}
	
	
	/**
	 * 发送微信消息模板
	 * 
	 */
	public String sendTemplate(String touser,String template_id,String page,String formid,JSONObject data) {
		String tepUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="
				+ getAccessToken();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(tepUrl);
		// 装配post请求参数
		JSONObject json = new JSONObject();
		json.put("touser", touser);
		json.put("template_id", template_id);
		json.put("page", page);
		json.put("form_id", formid);
		json.put("data", data);
		String resultStr = "发送失败";
		try {
			StringEntity myEntity = new StringEntity(json.toJSONString(), ContentType.APPLICATION_JSON);
			myEntity.setContentEncoding("UTF-8");
			myEntity.setContentType("application/json");
			// 设置post求情参数
			httpPost.setEntity(myEntity);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				// 发送成功
				String resutlEntity = EntityUtils.toString(httpResponse.getEntity());
				ResultTemplateDate resultTemplateDate = JSONObject.parseObject(resutlEntity, ResultTemplateDate.class);
				if (resultTemplateDate.getErrcode().equals("40037")) {
					resultStr = "template_id不正确";
				}
				if (resultTemplateDate.getErrcode().equals("41028")) {
					resultStr = "form_id不正确，或者过期";
				}
				if (resultTemplateDate.getErrcode().equals("41029")) {
					resultStr = "form_id已被使用";
				}
				if (resultTemplateDate.getErrcode().equals("41030")) {
					resultStr = "page不正确";
				}
				if (resultTemplateDate.getErrcode().equals("45009")) {
					resultStr = "接口调用超过限额（目前默认每个帐号日调用限额为100万）";
				}
				resultStr = "ok";
				return resultStr;
			} else {
				// 发送失败
				return resultStr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (httpClient != null) {
					// 释放资源
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultStr;
	}
	
	

}
